package com.kdtech.analyse.news;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class GzlightNewsAnalyse implements AnalyseNews {

	private static final String[] regex = { 
		
		"http://.*.gzlight.com/.*-[0-9]*.html",
		//http://info.gzlight.com/viewnews-4494.html
		
		
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news = new NewsMeta();
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Document doc = Jsoup.parse(htmltxt);
		news.setUrl(url);

		title = doc.select("h1").text();
		date = DateUtils.matchDate(doc.select("div#article_extinfo").text());
		content = HtmlCleaner.getContentHtml(url, doc.select("div#article_body"));
		author = JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		news.setAuthor(author);
		return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate() {
		return false;
	}

	public static void main(String[] args) {
		String url = "http://info.gzlight.com/viewnews-4536.html";
		GzlightNewsAnalyse analyse = new GzlightNewsAnalyse();
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta = CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

}
